class DataSamplesController < ApplicationController
  # GET /data_samples
  # GET /data_samples.xml
  def index
    @data_samples = DataSample.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @data_samples }
    end
  end

  # GET /data_samples/1
  # GET /data_samples/1.xml
  def show
    @data_sample = DataSample.find(params[:id])
    @chart_keys = @data_sample.iris_lookups.collect { |x| x.key }
    @chart_data = get_chart_data 

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @data_sample }
    end
  end

  # GET /data_samples/new
  # GET /data_samples/new.xml
  def new
    @data_sample = DataSample.new
    @available_datapoints = IrisLookup.find(:all, :select => "distinct key, description, id")

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @data_sample }
    end
  end

  # GET /data_samples/1/edit
  def edit
    @data_sample = DataSample.find(params[:id])
  end

  # POST /data_samples
  # POST /data_samples.xml
  def create
    @data_sample = DataSample.new(params[:data_sample])
    format.html { render :action => "new" } if not params[:follow_points]
    params[:follow_points].each do |point|
      @data_sample.iris_lookups << IrisLookup.find(:first, :conditions => {:key => point})
    end
    respond_to do |format|
      if @data_sample.save
        flash[:notice] = 'DataSample was successfully created.'
        format.html { redirect_to(@data_sample) }
        format.xml  { render :xml => @data_sample, :status => :created, :location => @data_sample }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @data_sample.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /data_samples/1
  # PUT /data_samples/1.xml
  def update
    @data_sample = DataSample.find(params[:id])

    respond_to do |format|
      if @data_sample.update_attributes(params[:data_sample])
        flash[:notice] = 'DataSample was successfully updated.'
        format.html { redirect_to(@data_sample) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @data_sample.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /data_samples/1
  # DELETE /data_samples/1.xml
  def destroy
    @data_sample = DataSample.find(params[:id])
    @data_sample.destroy

    respond_to do |format|
      format.html { redirect_to(data_samples_url) }
      format.xml  { head :ok }
    end
  end


  def get_chart_data
    data = {}
    points = []
    @data_sample.iris_lookups.each do |lookup|
      data[lookup.key] = Object::const_get(lookup.data_type.capitalize).find(:all, :conditions=>["created_at > :start_time AND created_at < :end_time AND key = :key", {:start_time => @data_sample.start, :end_time => @data_sample.end, :key => "#{lookup.address}-#{lookup.key}"}])
    end
    (1..data[data.keys[0]].length-1).each do |i|
      points << "['#{data[data.keys[0]][i].time.localtime.strftime("%A, %I:%M%p")}',#{data.keys.collect { |k| "#{(data[k][i].value - data[k][i-1].value)/33.333}," }}".chop + "],"
    end 
    points
  end
end
